#!/usr/bin/env ruby
#
#  Created by jack and phong on 2007-02-24.
#  Copyright (c) 2007. All rights reserved.

require 'TicTacToe'
require 'test/unit'

class TicTacToeTest <Test::Unit::TestCase
  def setup
    @game = TicTacToeBoard.new
  end
  
  def test_game_over
    got_here = true
    @game.gameOver
    assert(got_here)
  end
  
  def test_is_occupied_by_O
    @game.board[2] = 'O'
    assert(@game.isOccupied(2))
  end

  def test_is_occupied_by_X
    @game.board[6] = 'X'
    assert(@game.isOccupied(6))
  end
    
  def test_not_occupied
    got_here = true
    @game.board[4] = ''
    @game.isOccupied(4)
    assert(got_here)
  end
  
  def test_invalid_move_out_of_range1
    assert_raise(RuntimeError, "Invalid Move") { @game.validMove(-1,@game.currentPlayer) }
  end
  
  def test_invalid_move_out_of_range2
    assert_raise(RuntimeError, "Invalid Move") { 
      @game.validMove(10,@game.currentPlayer) || @game.validMove(9,@game.currentPlayer) 
    }
  end
  
  def test_invalid_move_occupied1
    @game.board[4] = 'X'
    assert(!@game.validMove(4,@game.currentPlayer))
  end
  
  def test_invalid_move_occupied2
    @game.board[8] = 'O'
    assert(!@game.validMove(8,@game.currentPlayer))
  end
  
  def test_invalid_move_player_skip_turns
    @game.board[1] = ''
    @game.board[2] = ''
    @game.validMove(1,@game.currentPlayer)
    
    assert_raise(RuntimeError, "Skipping turns.") { @game.validMove(2,(@game.currentPlayer+1)%2) }
  end
    
  def test_is_valid_move1
    @game.board[1] = ''
    assert(@game.validMove(1,@game.currentPlayer), "Invalid Move.")
  end
  
  def test_is_valid_move2
    @game.board[3] = ''
    assert(@game.validMove(3,@game.currentPlayer), "Invalid Move.")
  end
  
  def test_win_across1
    @game.board[0] = 'X'
    @game.board[1] = 'X'
    @game.board[2] = 'X'
    
    assert(@game.win('X'))
  end
  
  def test_win_across2
    @game.board[3] = 'O'
    @game.board[4] = 'O'
    @game.board[5] = 'O'
    
    assert(@game.win('O'))
  end
  
  def test_win_across3
    @game.board[6] = 'O'
    @game.board[7] = 'O'
    @game.board[8] = 'O'
    
    assert(@game.win('O'))
  end
  
  def test_win_across4
    @game.board[3] = 'X'
    @game.board[4] = 'X'
    @game.board[5] = 'X'
    
    assert(@game.win('X'))
  end
  
  def test_win_down1
    @game.board[0] = 'O'
    @game.board[3] = 'O'
    @game.board[6] = 'O'
    
    assert(@game.win('O'))
  end
  
  def test_win_down2
    @game.board[1] = 'O'
    @game.board[4] = 'O'
    @game.board[7] = 'O'
    
    assert(@game.win('O'))
  end
  
  def test_win_down3
    @game.board[2] = 'X'
    @game.board[5] = 'X'
    @game.board[8] = 'X'
    
    assert(@game.win('X'))
  end
  
  def test_win_diagnals1
    @game.board[0] = 'X'
    @game.board[4] = 'X'
    @game.board[8] = 'X'

    assert(@game.win('X'))
  end
  
  def test_win_diagnals2
    @game.board[2] = 'X'
    @game.board[4] = 'X'
    @game.board[6] = 'X'

    assert(@game.win('X'))
  end
  
  def test_not_win1
    @game.board[6] = 'O'
    @game.board[4] = 'O'
    @game.board[7] = 'O'
    
    assert(!@game.win('O'))
  end
  
  def test_not_win2
    @game.board[0] = 'O'
    @game.board[7] = 'O'
    @game.board[6] = 'O'
    
    assert(!@game.win('O'))
  end
  
  def test_not_win3
    @game.board[2] = 'X'
    @game.board[6] = 'X'

    assert(!@game.win('X'))
  end
  
  def test_game_not_over1
    @game.board[0] = 'X'
    @game.board[1] = 'O'
    @game.board[2] = 'X'
    @game.board[3] = 'O'
    @game.board[4] = 'X'
    @game.board[5] = 'O'
    @game.board[6] = 'O'
    @game.board[7] = 'X'
    
    # X O X
    # O X O
    # O X 

    assert(!@game.gameOver)
  end
  
  def test_game_not_over2
    @game.board[2] = 'X'
    @game.board[4] = 'X'
    @game.board[2] = 'O'
    @game.board[6] = 'O'

    assert(!@game.gameOver)
  end
  
  def test_game_Over1
    @game.board[2] = 'X'
    @game.board[4] = 'X'
    @game.board[6] = 'X'

    assert(@game.gameOver)
  end
  
  def test_game_Over2
    @game.board[3] = 'O'
    @game.board[4] = 'O'
    @game.board[5] = 'O'
    @game.board[2] = 'X'
    @game.board[7] = 'X'

    assert(@game.gameOver)
  end
  
  def test_game_Over3
    @game.board[1] = 'X'
    @game.board[4] = 'X'
    @game.board[7] = 'X'

    assert(@game.gameOver)
  end
  
  def test_game_Over4
    @game.board[8] = 'O'
    @game.board[4] = 'O'
    @game.board[0] = 'O'

    assert(@game.gameOver)
  end
  
  def test_game_Over5
    @game.board[0] = 'X'
    @game.board[1] = 'O'
    @game.board[2] = 'X'
    @game.board[3] = 'O'
    @game.board[4] = 'X'
    @game.board[5] = 'O'
    @game.board[6] = 'O'
    @game.board[7] = 'X'
    @game.board[8] = 'O'
    
    # X O X
    # O X O
    # O X O

    assert(@game.gameOver)
  end
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
end
